package com.hhlkeepalive.onePx;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import java.lang.ref.WeakReference;

public class ScreenManager {
    private Context mContext;

    private WeakReference<Activity> mWeakReference;

    private static ScreenManager mScreenManager;

    private ScreenManager(Context context) {
        mContext = context;
    }

    public static ScreenManager getInstance(Context context) {
        if (mScreenManager == null) {
            synchronized (ScreenManager.class) {
                if (mScreenManager == null) {
                    mScreenManager = new ScreenManager(context);
                }
            }
        }
        return mScreenManager;
    }

    public void setSinglePixelActivity(Activity activity) {
        mWeakReference = new WeakReference<>(activity);
    }

    public void startSinglePixelActivity(Activity from) {
        Intent intent = new Intent(from, SinglePixelActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }

    public void finishSinglePixelActivity() {
        if (mWeakReference != null &&mWeakReference.get() != null) {
            mWeakReference.get().finish();
        }
    }

}
